package com.bjsxt.common.util;

import javax.annotation.Resource;

import org.apache.catalina.tribes.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
@Component("encrypt")
public class Encrypt  
{	
	@Resource
	private Md5 md5;
	@Resource
	private RegexUtil regexUtil;
	/* 
	 * 明文:明文是32位随机数+$md5$+password
	 * 
	 * 密文:密文是32位随机数+$md5$+MD5（32位随机数+$md5$+password）
	 *  
	 *  */
	public String encryptPassword(String password)
	{	
		/* 先得到32为随机数 */
		String randomStr = regexUtil.getRandom(32);
		String randomStr1=randomStr;
		System.out.println(randomStr);
		String md5Str = randomStr + "$MD5$"+password; 
		/* 将32+$md5$+password用MD5加密 */
		randomStr=md5.md5(md5Str);
		/* 拼接密文 */
		randomStr=randomStr1+"$MD5$"+randomStr;
		System.out.println(randomStr);
		return randomStr;
	}
	/**
	 * 验证密码将密文和用户的密码再按照加密的方法走一次 
	 * 看看是否一样 一样的话就是密码正确
	 * @param password
	 * @param encryptPassord
	 */
	public boolean checkPassword(String password,String encryptPassword)
	{
		String[] encStrs = encryptPassword.split("\\$");
		System.out.println(encStrs[0]);
		String md5Str = encStrs[0] + "$" + encStrs[1] + "$" + password ; 
		md5Str = encStrs[0] + "$" + encStrs[1] + "$" + this.md5.md5(md5Str);
		System.out.println(md5Str);
		if(md5Str.equals(encryptPassword)){
			return true;
		}
		return false;
	}
}	
